package completablefuture;

import java.text.SimpleDateFormat;
import java.util.Arrays;
import java.util.Date;
import java.util.List;

public class EngineDemo {

    private static void sleep(long sleepTime) {
        try {
            Thread.sleep(sleepTime);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }

    private static void currentDate(String str) {
        // 创建一个Date对象，它包含了当前时间
        Date now = new Date();
        // 创建一个SimpleDateFormat对象，用于指定输出格式
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        // 使用format方法将Date对象格式化为字符串
        String currentTime = dateFormat.format(now);
        // 打印当前时间
        System.out.println(str + "是: " + currentTime);
    }

    public static void main(String[] args) {
        currentDate("执行前");
        List<Integer> numList = CompletableFutureEngine.parallelFutureJoin(Arrays.asList(1, 3, 5),
                num -> {
                    sleep(num * 1000);
                    if (num == 1 || num == 3) {
                        throw new BusinessException("心别太大");
                    }
                    return num;
                }, e -> {
                    if (e instanceof BusinessException) {
                        System.out.println("BusinessException = " + e.getMessage());
                    } else {
                        System.out.println("Not BusinessException");
                    }
                });
        System.out.println(numList);
        currentDate("执行后");

    }
}
